package com.atguigu.fink.chapter02;

import com.atguigu.fink.bean.WaterSensor;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.functions.TableAggregateFunction;
import org.apache.flink.util.Collector;

import static org.apache.flink.table.api.Expressions.$;
import static org.apache.flink.table.api.Expressions.call;

/**
 * @Author lzc
 * @Date 2022/11/28 09:06
 */
public class Flink04_TableAgg {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        DataStreamSource<WaterSensor> stream = env.fromElements(
            new WaterSensor("s1", 1L, 10),
            new WaterSensor("s1", 2L, 8),
            new WaterSensor("s1", 3L, 40),
            new WaterSensor("s1", 4L, 20),
            new WaterSensor("s1", 5L, 40),
            new WaterSensor("s1", 6L, 50)
        );
        
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        Table table = tEnv.fromDataStream(stream);
        tEnv.createTemporaryView("sensor", table);
        
        // 1. table api 中使用
        // 1.1 内联用法
        table
            .groupBy($("id"))
            .flatAggregate(call(Top2.class, $("vc")))
            .select($("id"), $("rank"), $("vc"))
            .execute()
            .print();
        // 1.2. 先注册再使用
        
        // 2. 在sql 中使用
        // 目前还不支持
        
    }
    
    @AllArgsConstructor
    @NoArgsConstructor
    @Data
    public static class Result {
        private String rank;
        private Integer vc;
    }
    
    @AllArgsConstructor
    @NoArgsConstructor
    @Data
    public static class FirstSecond {
        private Integer first = Integer.MIN_VALUE;
        private Integer second = Integer.MIN_VALUE;
    }
    
    public static class Top2 extends TableAggregateFunction<Result, FirstSecond> {
        
        // 创建累加器
        @Override
        public FirstSecond createAccumulator() {
            return new FirstSecond();
        }
        
        // 累加的过程
        public void accumulate(FirstSecond fs, Integer vc) {
            if (vc > fs.getFirst()) {
                fs.setSecond(fs.getFirst());
                fs.setFirst(vc);
            } else if (vc > fs.getSecond()) {
                fs.setSecond(vc);
            }
        }
        
        // 最终的结果
        // 方法名: emitValue
        // 参数 1: 必须是累加器
        // 参数 2:必须是Collector<Result>
        public void emitValue(FirstSecond fs, Collector<Result> out) {
            out.collect(new Result("第一名", fs.getFirst()));
            
            if (fs.getSecond() > Integer.MIN_VALUE) {
                out.collect(new Result("第二名", fs.getSecond()));
            }
        }
    }
}
/*

new WaterSensor("s1", 1L, 10),
    第一名     10
new WaterSensor("s1", 2L, 8),
    第一名     10
    第二名     8
new WaterSensor("s1", 3L, 40),
    第一名     40
    第二名     10
....
 */